package com.example.bilibili.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.bilibili.Constants;
import com.example.bilibili.GoodsBean;
import com.example.bilibili.GoodsInfoActivity;
import com.example.bilibili.ResultBeanData;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bilibili.R;
import com.example.bilibili.SousuoActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.listener.OnLoadImageListener;

import java.util.ArrayList;
import java.util.List;

public class TuijianFragmentAdapter extends RecyclerView.Adapter{

    public static final int BANNER = 0;
    public static final int HOT = 1;

    private LayoutInflater mLayoutInflater;
    private Context mContext;

    private ResultBeanData.ResultBean resultBean;


    private int currentType = BANNER;

    public TuijianFragmentAdapter(Context mContext, ResultBeanData.ResultBean resultBean) {
        this.mContext = mContext;
        this.resultBean = resultBean;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(mContext, mLayoutInflater.inflate(R.layout.banner_viewpager, null));
        }
        else if(viewType == HOT){
            return new HotViewHolder(mContext, mLayoutInflater.inflate(R.layout.hot_item, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == BANNER) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getBanner_info());
        }
        else if(getItemViewType(position)==HOT){
            HotViewHolder hotViewHolder = (HotViewHolder) holder;
            hotViewHolder.setData(resultBean.getHot_info());
        }
    }


    private class BannerViewHolder extends RecyclerView.ViewHolder {
        private Context mContext;
        private Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.banner = (Banner) itemView.findViewById(R.id.banner);
        }


        public void setData(List<ResultBeanData.ResultBean.BannerInfoBean> banner_info) {
            //设置Banner的数据
            //得到图片集合地址
            List<String> imagesUrl = new ArrayList<>();
            for (int i = 0; i < banner_info.size(); i++) {
                String imageUrl = banner_info.get(i).getImage();
                imagesUrl.add(imageUrl);
            }

            //设置循环指示点
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            banner.setImages(imagesUrl, new OnLoadImageListener() {
                @Override
                public void OnLoadImage(ImageView view, Object url) {

                    //联网请求图片-Glide
                    Glide.with(mContext).load(Constants.BASE_IMAGE_URL + url).into(view);

                }
            });

            //设置item的点击事件
            banner.setOnBannerClickListener(new OnBannerClickListener() {
                @Override
                public void OnBannerClick(int position) {
                    Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
                    //startGoodsInfoActivity(goodsBean);
                }
            });
        }
    }

    private void startGoodsInfoActivity(GoodsBean goodsBean) {
        Intent intent = new Intent(mContext, GoodsInfoActivity.class);
        intent.putExtra("GOODS_BEAN",goodsBean);
        mContext.startActivity(intent);
    }


    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
//            case CHANNEL:
//                currentType = CHANNEL;
//                break;
//            case ACT:
//                currentType = ACT;
//                break;
//            case SECKILL:
//                currentType = SECKILL;
//                break;
//            case RECOMMEND:
//                currentType = RECOMMEND;
//                break;
            case HOT:
                currentType = HOT;
                break;
        }
        return currentType;
    }

    @Override
    public int getItemCount() {
        //开发过程中从1-->6
        return 2;
    }


    class HotViewHolder extends RecyclerView.ViewHolder{

        private final Context mContext;

        private GridView gv_hot;

        public HotViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            gv_hot = (GridView) itemView.findViewById(R.id.gv_hot);

        }

        public void setData(final List<ResultBeanData.ResultBean.HotInfoBean> hot_info) {
            //1.有数据
            //2.设置GridView的适配器
            HotGridViewAdapter  adapter = new HotGridViewAdapter(mContext,hot_info);
            gv_hot.setAdapter(adapter);


            //设置item的监听
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                    Toast.makeText(mContext, "position=="+position, Toast.LENGTH_SHORT).show();

                    ResultBeanData.ResultBean.HotInfoBean hotInfoBean =  hot_info.get(position);

                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setFensi(hotInfoBean.getFensi());
                    goodsBean.setBofangliang(hotInfoBean.getBofangliang());
                    goodsBean.setCommennt(hotInfoBean.getCommennt());
                    goodsBean.setFensi(hotInfoBean.getFigure());
                    goodsBean.setName(hotInfoBean.getName());
                    goodsBean.setTime(hotInfoBean.getTime());
                    goodsBean.setRed(hotInfoBean.getRed());
                    goodsBean.setTouxiang(hotInfoBean.getTouxiang());
                    goodsBean.setShijian(hotInfoBean.getShijian());
                    goodsBean.setFensi(hotInfoBean.getFensi());
                    goodsBean.setDianzan(hotInfoBean.getDianzan());
                    goodsBean.setToubi(hotInfoBean.getToubi());
                    goodsBean.setShoucang(hotInfoBean.getShoucang());
                    goodsBean.setZhuanfa(hotInfoBean.getZhuanfa());
                    goodsBean.setShipin(hotInfoBean.getShipin());
                    goodsBean.setId(hotInfoBean.getId());
                    goodsBean.setC1(hotInfoBean.getC1());
                    goodsBean.setC2(hotInfoBean.getC2());
                    goodsBean.setC3(hotInfoBean.getC3());
                    goodsBean.setHotposition(hotInfoBean.getHotposition());
                    startGoodsInfoActivity(goodsBean);
                }
            });
        }
    }
}

